﻿using Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xiaoxiong.KYJDAL.Models;

namespace xiaoxiong.KYJDAL.MyDbcontext
{
    public class kyjDbContext : IDisposable
    {
        public SqlSugarClient Db { get; }

        public kyjDbContext()
        {

            Db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.MySql,
                ConnectionString = "server=localhost;Database=KYJDb;Uid=root;Pwd=root",
                IsAutoCloseConnection = true
            });

            Db.Aop.OnLogExecuted = (sql, pars) =>
            {
                Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
            };
        }

        private void Init()
        {
            Db.DbMaintenance.CreateDatabase();//创建库
            //建表
            Db.CodeFirst.InitTables<SysAdmin>();
            Db.CodeFirst.InitTables<SysLog>();
        }

        public void Dispose()
        {
            this.Db.Dispose();
        }
    }
}
